package io.aicn.lowcode.controller;

import io.aicn.lowcode.dto.PageResponse;
import io.aicn.lowcode.dto.UserCreate;
import io.aicn.lowcode.dto.UserUpdate;
import io.aicn.lowcode.entity.User;
import io.aicn.lowcode.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
@Tag(name = "User Management", description = "User CRUD operations")
public class UserController {
    
    @Autowired
    private IUserService userService;
    
    @GetMapping
    @Operation(summary = "Get all users", description = "Retrieve a list of all users in the system")
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok(users);
    }
    
    @PostMapping
    @Operation(summary = "Create a new user", description = "Add a new user to the system")
    public ResponseEntity<User> createUser(@Valid @RequestBody UserCreate userCreate) {
        User user = userService.createUser(userCreate);
        return ResponseEntity.status(HttpStatus.CREATED).body(user);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "Get user by ID", description = "Retrieve a specific user by their ID")
    public ResponseEntity<User> getUserById(
            @Parameter(description = "User ID") @PathVariable Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(user);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "Update user", description = "Update an existing user's information")
    public ResponseEntity<User> updateUser(
            @Parameter(description = "User ID") @PathVariable Long id,
            @Valid @RequestBody UserUpdate userUpdate) {
        User user = userService.updateUser(id, userUpdate);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(user);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "Delete user", description = "Delete a user from the system")
    public ResponseEntity<Void> deleteUser(
            @Parameter(description = "User ID") @PathVariable Long id) {
        boolean deleted = userService.deleteUser(id);
        if (!deleted) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.noContent().build();
    }
    
    @GetMapping("/search")
    @Operation(summary = "Search users", description = "Search users by various criteria")
    public ResponseEntity<List<User>> searchUsers(
            @Parameter(description = "User name (partial match)") @RequestParam(required = false) String name,
            @Parameter(description = "User email (partial match)") @RequestParam(required = false) String email,
            @Parameter(description = "Minimum age") @RequestParam(required = false) Integer minAge,
            @Parameter(description = "Maximum age") @RequestParam(required = false) Integer maxAge) {
        List<User> result = userService.searchUsers(name, email, minAge, maxAge);
        return ResponseEntity.ok(result);
    }
}